package com.scalar.forecast.salesForecastEntry;

import com.scalar.forecast.commons.logger.Logger;
import com.scalar.forecast.commons.logger.LoggerManager;

import java.util.LinkedList;

/**
 * User: Sujan
 * Date: Jul 13, 2005
 * Time: 6:35:00 PM
 *
 *  <BR>
 * This is a utility linked list class. This class just overrides the get(int) method of LinkedList.<br>
 * It just creates the object specified in the cunstructor and returns if the object not present at the specified index.<br>
 * The main purpose of creating this class is to avoid the scope <br>session</br> when we used lists with nested tags.
 *
 */
public class AutoGrowingLinkedList extends LinkedList
{
    private Class clazz;
    private static final Logger LOGGER = LoggerManager.getLogger( AutoGrowingLinkedList.class );
	public AutoGrowingLinkedList ( Class clazz )
	{
		super();
		this.clazz = clazz;
	}

	public Object get ( int index )
	{
		Object obj = null;
		int size = super.size();
        LOGGER.log ( Logger.INFO, "In AutoGrowingLinkedList.get() with size:"+ size +"index:" + index);
		if ( index < size )
		{
			obj = super.get ( index );
		}
		else
		{
			LOGGER.log ( Logger.INFO, "in else" );
			for ( int i = size; i <= index; i++ )
			{
				obj = null;
				try
				{
					obj = this.clazz.newInstance ();
				}
				catch ( Exception e )
				{
                    LOGGER.log( Logger.ERROR, "exception in AutoGrowingLinkedList.get(.)", e );
					e.printStackTrace();
				}
				super.add ( obj );
			}
		}
		return obj;
	}
}
